﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using DBFunc;

namespace Lab_1_MultiDBAccess
{
    public partial class frmMain : Form
    {
        private FilialDBFunc FFunc;

        public frmMain()
        {
            InitializeComponent();
            try
            {
                FFunc = new FilialDBFunc(@"Data Source=NOUT-PC;Initial Catalog=Filial; User Id=Test; Password=Test;");
                gvData.DataSource = FFunc.GetAllProducts();
                lbStatus.Text = "Подключено\nсервер:NOUT-PC\nБаза данных: Filial";
                pnlEdit.Visible = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message,"Предупреждение");
            }
        }


        private void btnInsert_Click(object sender, EventArgs e)
        {
            FFunc.UpdateProducts();
        }


        private void btnDelete_Click(object sender, EventArgs e)
        {
            FFunc.DelProduct(tbDelID.Text);
        }


        private void btnConnect_Click_1(object sender, EventArgs e)
        {
            ConnForm CnForm = new ConnForm();
            if (CnForm.ShowDialog() == DialogResult.OK)
            {
               try
                {
                    FFunc = new FilialDBFunc(CnForm.connStr);
                    gvData.DataSource = FFunc.GetAllProducts();
                    lbStatus.Text = "Подключено к Базе данных";
                    pnlEdit.Visible = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(String.Format("Connection Error\n Reason:{0}", ex.Message), "Error!");
                    pnlEdit.Visible = false;
                    lbStatus.Text = "Не подключено\nк базе данных";
                }
            }
            
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            //gvData = new Grid
            DataTable dt = FFunc.GetAllProducts();
            gvData.DataSource = dt;
            gvData.Refresh();
        }
    }
}
